package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.dtos.WmCommentStatusDto;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {
   @Autowired
   private ApArticleConfigMapper apArticleConfigMapper;


    @Override
    public void updateDownOrUp(Map<String, Object> map) {
        //取值
        Long articleId = (Long)map.get("articleId");
        Integer enable = (Integer)map.get("enable");

        if(articleId==null || enable==null){
            return;
        }

        UpdateWrapper<ApArticleConfig> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("article_id",articleId);
        updateWrapper.set("is_down",enable==1? false : true);
        update(updateWrapper);
    }

    /**
     * 修改文章可评论状态
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateArticleConfigStatus(WmCommentStatusDto dto) {
            //取值
//        Integer articleId = dto.getArticleId();
        Long articleId = dto.getArticleId();
        Integer operation = dto.getOperation();
//        if (articleId == null || operation == null){
//            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
//        }
        LambdaUpdateWrapper<ApArticleConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ApArticleConfig::getArticleId,articleId);
        wrapper.set(ApArticleConfig::getIsComment,operation==1?true:false);
        boolean update = update(wrapper);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (update){
            responseResult.setErrorMessage("修改成功");
        }


        return responseResult;
    }

    /**
     *
     * @param map
     */
    @Override
    public void isDelete(Map<String, Object> map) {
        //取值
        Long articleId = (Long)map.get("articleId");
//        Integer isDelete = (Integer)map.get("isDelete");

        if(articleId==null){
            return;
        }

        UpdateWrapper<ApArticleConfig> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("article_id",articleId);
        updateWrapper.set("is_delete",true);

        update(updateWrapper);
    }

    //查看文章是否可以评论
    @Override
    public Boolean isComment(Long articleId) {

        boolean isComment = false;

        LambdaQueryWrapper<ApArticleConfig> query = new LambdaQueryWrapper<>();
        query.eq(ApArticleConfig::getArticleId,articleId);
        ApArticleConfig articleConfig = getOne(query);

         isComment = articleConfig.getIsComment();
        return isComment;
    }
}
